<?php  
// 1.连接数据库
$connect = mysqli_connect('localhost','root','123456','users');
// var_dump($connect);
// 判断
if (!$connect) {
  exit('<h1>连接数据库失败</h1>');
}

// 获取当前页数
$page = empty($_GET['page']) ? 1 : $_GET['page'];

// 要跳过的数据
$cross =($page-1)*5;
// echo $page,$cross;

// 2.执行查询操作

// 获取数据
$query1 = mysqli_query($connect,"select * from info limit {$cross},5;");

// 获取数据长度
$query2 = mysqli_query($connect,'select count(1) as count from info');
// var_dump($query2);
$len = mysqli_fetch_assoc($query2);
// echo $len['count'];

// var_dump($query1);
// 判断
if (!$query1 || !$query2) {
  exit('<h1>查询失败</h1>');
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>XXX管理系统</title>
  <link rel="stylesheet" href="assets/css/bootstrap.css">
  <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
  <nav class="navbar navbar-expand navbar-dark bg-dark fixed-top">
    <a class="navbar-brand" href="#">XXX管理系统</a>
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="index.html">用户管理</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">商品管理</a>
      </li>
    </ul>
  </nav>
  <main class="container">
    <h1 class="heading">用户管理 <a class="btn btn-primary" href="add.php">添加</a></h1>
    <table class="table table-hover">
      <thead>
        <tr>
          <th>#</th>
          <th>头像</th>
          <th>姓名</th>
          <th>性别</th>
          <th>年龄</th>
          <th class="text-center" width="140">操作</th>
        </tr>
      </thead>
      <tbody>
        <!-- 3.遍历查询对象 -->
        <?php while($item = mysqli_fetch_assoc($query1)): ?>
          <tr>
            <th><?php echo $item['id']; ?></th>
            <td><img src = "<?php echo $item['avatar']; ?>"></td>
            <td><?php echo $item['name']; ?></td>
            <td><?php echo $item['gender'] == 0? '♂': '♀'; ?></td>
            <td><?php echo floor((time()-strtotime($item['birthday']))/60/60/24/30/12); ?></td>
            <td class="text-center">
              <a class="btn btn-info btn-sm" href="edit.php?id=<?php echo $item['id'] ?>">编辑</a>
              <a class="btn btn-danger btn-sm" href="delete.php?id=<?php echo $item['id'] ?>">删除</a>
            </td>
          </tr>
        <?php endwhile ?>
      </tbody>
    </table>
    <ul class="pagination justify-content-center">
      <li class="page-item"><a class="page-link" href="index.php?page=1">&laquo;</a></li>
      <?php for ($i=0; $i < ceil($len['count']/5); $i++): ?>
        <li class="page-item"><a class="page-link" href="index.php?page=<?php echo $i+1; ?>"><?php echo $i+1; ?></a></li>
      <?php endfor ?>
      <li class="page-item"><a class="page-link" href="index.php?page=<?php echo ceil($len['count']/5); ?>">&raquo;</a></li>
    </ul>
    <!-- 4.释放数据库内存 -->
    <?php mysqli_free_result($query1);mysqli_free_result($query2); ?>
    <!-- 5.关闭连接通道 -->
    <?php mysqli_close($connect); ?>
  </main>
</body>
</html>
